<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

/**
 * @property string id 唯一id
 * @property string user_id 发布者
 * @property string category_id 所属分类
 * @property string tag 标签
 * @property string title 标题
 * @property string content 内容
 * @property string is_show 是否显示
 * @property string is_top 是否置顶
 */
class Article extends Model
{
    protected $table = 'articles';

    protected $fillable = [
        'user_id', 'category_id', 'title', 'content', 'is_show'
    ];

    const RICH = 10; //财富积分

    public static function createArticle($data)
    {
        $data['user_id'] = Auth::id();
        $data['is_show'] = 1;
        self::create($data);
    }
    protected static function boot()
    {
        parent::boot(); // TODO: Change the autogenerated stub
        static::creating(function(){
            User::query()->find(Auth::id())->increment('rich',self::RICH);
        });
    }

    public function comments()
    {
        return $this->hasMany(Comment::class, 'article_id', 'id');
    }

    public function author()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function category()
    {
        return $this->belongsTo(Categories::class, 'category_id', 'id');
    }

    public function scopeCategory($query,$param){
        if($param){
            return $query->where('articles.category_id',$param);
        }
        return $query;
    }

    /**
     * @param $query
     * @return mixed
     */
    public function scopeResentArticle($query)
    {
        return $query->orderBy('created_at', 'desc')->where('is_show',1);
    }
}
